<template>
  <div class="changePassword">
    <div class="circle-button" @click="closePass">
      <div class="close-icon">×</div>
    </div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off" placeholder="password" id="password"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="checkPassword" id="pass2"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" class="button" id="submit">提交</el-button>
        <el-button @click="resetForm('ruleForm')" class="button" id="clear">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import axios from "axios";
import { serverAddress } from "../js/config";
export default {
    name:'ChangePassword',
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' },
            {min: 10, max: 16, message: "请输入10-16位的密码", trigger: "blur" }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      closePass(){
        this.$emit('closeChangePass');
      },
      submitForm(formName) {
        var api=`${serverAddress}/update/teacherNewPwd`;
        var information={
          teacherId:sessionStorage.getItem('Id'),
          teacherPwd:this.ruleForm.pass,
        }
        if(sessionStorage.getItem('管理员')==='管理员'){
          api=`${serverAddress}/update/rootNewPwd`;
          information={
            rootId:sessionStorage.getItem('Id'),
            rootPwd:this.ruleForm.pass,
        }
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios({
              url:api,
              /* url:'https://mock.apifox.com/m2/3662090-0-default/130087726', */
              method:'POST',
              data:information,
            }).then((response)=>{
                if(response.data.code===1){
                  console.log(response.data);
                  this.resetForm('ruleForm');
                  this.$emit('closeChangePass');
                  alert('修改成功！');
                }else{
                  console.log(response.data);
                  this.resetForm('ruleForm');
                  alert('修改失败，请重新尝试！');
                }
              })
          } else {
            alert('error submit!!');
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style scoped>
.button{
  text-align: center;
  padding: 7px;
  border-radius: 23px;
  font-weight: 500;
  font-size: 17px;
  margin: 0 auto;
  width: 30%;
  margin-top: 8px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
}
.button#submit{
  position: absolute;
  left:6px;
}
.button#clear{
  position: absolute;
  left:120px;
}

.changePassword{
  background-color: rgb(255, 255, 255);
}
.el-form{
  height:150px;
  position: relative;
  top:19%;
  left: -7%;
}
.el-form-item::v-deep .el-input__inner {
  width: 235px;
  background-color: rgb(255, 255, 255);
}
.el-input{
  height:0px;
  padding-bottom: 50px;
  position: relative;
  left: -5%;
}
.circle-button {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: absolute;
  right: 0;
}

.close-icon {
  color: #fbc2eb; /* “×”符号的颜色 */
  font-size: 20px;
  font-weight: bold;
}

</style>